<?php
/**
 * Created by JetBrains PhpStorm.
 * User: alexey
 * Date: 12/30/11
 * Time: 9:14 AM
 * To change this template use File | Settings | File Templates.
 */
class WorkTimeWidget extends CWidget
{
    public static $WEEK_DAYS = array(
        '1' => 'mon',
        '2' => 'tue',
        '3' => 'wed',
        '4' => 'thu',
        '5' => 'fri',
        '6' => 'sat',
        '7' => 'sun',
    );
    public $htmlOptions;

    public $items;
    public $post_id;

    /**
     * Initializes the view.
     * This method will initialize required property values and instantiate {@link columns} objects.
     */
    public function init()
    {
        $this->htmlOptions['id'] = $this->getId();
    }

    /**
     * Renders the view.
     * This is the main entry of the whole view rendering.
     * Child classes should mainly override {@link renderContent} method.
     */
    public function run()
    {
        $this->renderMain();

    }

    public function renderMain()
    {
        echo CHtml::openTag('div', array('class'=>'work_time'));
        $this->renderHeader();
        $workTimes = YumWorkTime::loadSet($this->post_id);
        foreach ($workTimes as $workTime) {
            $this->renderItem($workTime);
        }
        echo CHtml::closeTag('div');
    }

    public function renderHeader()
    {
        echo CHtml::openTag('div',array('class'=>'header row'));

        echo CHtml::openTag('div',array('class'=>'coll_day'));
        echo Yum::t('day');
        echo CHtml::closeTag('div');

        echo CHtml::openTag('div',array('class'=>'coll_work_time'));
        echo Yum::t('work time');
        echo CHtml::closeTag('div');

        echo CHtml::openTag('div',array('class'=>'coll_free_day'));
        echo Yum::t('free day');
        echo CHtml::closeTag('div');

        echo CHtml::openTag('div',array('class'=>'coll_all_day'));
        echo Yum::t('all day');
        echo CHtml::closeTag('div');

        echo CHtml::closeTag('div');
    }

    public function renderItem($workTime)
    {
        echo CHtml::openTag('div',array('class'=>'row'));

        echo CHtml::hiddenField('WorkTimes['.$workTime->id.'][id]',$workTime->id);
        echo CHtml::hiddenField('WorkTimes['.$workTime->id.'][week_day]',$workTime->weekDay->id);
        //echo CHtml::hiddenField('WorkTimes['.$workTime->id.'][post_id]',$workTime->post->id);

        echo CHtml::openTag('div',array('class'=>'coll_day'));
        echo CHtml::label(Yum::t($workTime->weekDay->name), null);
        echo CHtml::closeTag('div');

        echo CHtml::openTag('div',array('class'=>'coll_work_time'));
        echo CHtml::radioButton('WorkTimes['.$workTime->id.'][type]', $workTime->isTime(),array('value'=>1));
        echo CHtml::dropDownList('WorkTimes['.$workTime->id.'][start]', $workTime->start_time_id, YumTime::loadItems() );
        echo ' - ';
        echo CHtml::dropDownList('WorkTimes['.$workTime->id.'][end]', $workTime->end_time_id, YumTime::loadItems());

        echo CHtml::closeTag('div');

        echo CHtml::openTag('div',array('class'=>'coll_free_day'));
        echo CHtml::radioButton('WorkTimes['.$workTime->id.'][type]', $workTime->isFree(),array('value'=>2));
        echo CHtml::closeTag('div');

        echo CHtml::openTag('div',array('class'=>'coll_all_day'));
        echo CHtml::radioButton('WorkTimes['.$workTime->id.'][type]', $workTime->isAll(),array('value'=>3));
        echo CHtml::closeTag('div');

        echo CHtml::closeTag('div');
    }

    public static function saveAll($workTimes, $postId) {
        foreach ($workTimes as $workTime) {
            $yumWorkTime = YumWorkTime::model()->findByPk($workTime['id']);
            $yumWorkTime->post_id=$postId;
            $yumWorkTime->type_id=$workTime['type'];
            $yumWorkTime->start_time_id=$workTime['start'];
            $yumWorkTime->end_time_id=$workTime['end'];
            $yumWorkTime->week_day_id=$workTime['week_day'];
            $yumWorkTime->save();
        }
    }
}

